package net.zdreamer.shunyi.ui.fragment;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import net.zdreamer.shunyi.R;
import net.zdreamer.shunyi.adapter.GuangChangAdapter;
import net.zdreamer.shunyi.myview.XListView;
import net.zdreamer.shunyi.ui.SocialActivity;
import net.zdreamer.shunyi.ui.myinfo.XiangQingAcitivity;
import net.zdreamer.shunyi.utils.ListUtils.GuangChangInfos;


public class BangHuiGuangChangFragment extends Fragment implements View.OnClickListener,
        XListView.IXListViewListener, AdapterView.OnItemClickListener {
    private ImageView jiaoyou;
    private Intent intent;
    private XListView guangchanglist;

    private GuangChangInfos guangChangInfos;
    private GuangChangAdapter mAdapter;
    private Handler mHandler;
    private int start = 0;
    private static int refreshCnt = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_bang_hui_guang_chang, container, false);
        guangchanglist = (XListView) view.findViewById(R.id.guangchanglist);
        guangchanglist.addHeaderView(inithaed());
        guangchanglist.setOnItemClickListener(this);
        guangchanglist.setXListViewListener(this);
        guangchanglist.setPullLoadEnable(true);
        return view;
    }


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

    }

    @Override
    public void onStart() {
        super.onStart();
        mHandler = new Handler();

        guangChangInfos = new GuangChangInfos();
        mAdapter = new GuangChangAdapter(getActivity(), guangChangInfos.guangChangInfos, R.layout.guangchamg_item);
        guangchanglist.setAdapter(mAdapter);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.guangchangjiaoyou:
                intent = new Intent(getActivity(), SocialActivity.class);
                startActivity(intent);
                break;
            default:
                break;
        }
    }

    private View inithaed() {
        LayoutInflater inflater = LayoutInflater.from(getActivity());
        View view = inflater.inflate(R.layout.guangchanghead, null);
        jiaoyou = (ImageView) view.findViewById(R.id.guangchangjiaoyou);
        jiaoyou.setOnClickListener(this);
        return view;
    }

    private void onLoad() {
        guangchanglist.stopRefresh();
        guangchanglist.stopLoadMore();
        guangchanglist.setRefreshTime("刚刚");
    }

    @Override
    public void onRefresh() {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                start = ++refreshCnt;
                onLoad();
            }
        }, 2000);
    }

    @Override
    public void onLoadMore() {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                onLoad();
            }
        }, 2000);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        intent = new Intent(getActivity(), XiangQingAcitivity.class);
        startActivity(intent);
    }
}
